1 module hip.filesystem.systems.android; 2 import hip.api.filesystem.hipfs; 3 import hip.filesystem.hipfs; 4 import hip.error.handler; 5 6 version(Android) 7 { 8 public import hip.jni.android.asset_manager; 9 public import hip.jni.android.asset_manager_jni; 10 __gshared AAssetManager* aaMgr; 11 class HipAndroidFile : HipFile 12 { 13 import core.stdc.stdio; 14 AAsset* asset; 15 @disable this(); 16 this(string path, FileMode mode) 17 { 18 super(path, mode); 19 } 20 override ulong getSize() 21 { 22 ErrorHandler.assertLazyErrorMessage(asset != null, "HipAndroidFile error", 23 "Can't get size from null asset '"~path~"'"); 24 return cast(ulong)AAsset_getLength64(asset); 25 } 26 override bool open(string path, FileMode mode) 27 { 28 import hip.util.string; 29 asset = AAssetManager_open(aaMgr, path.toStringz, AASSET_MODE_BUFFER); 30 return asset != null; 31 } 32 override int read(void* buffer, ulong count) 33 { 34 ErrorHandler.assertErrorMessage(asset != null, "HipAndroidFile error", "Can't read null asset"); 35 return AAsset_read(asset, buffer, count); 36 } 37 override long seek(long count, int whence = SEEK_CUR) 38 { 39 ErrorHandler.assertErrorMessage(asset != null, "HipAndroidFile error", "Can't seek null asset"); 40 super.seek(count, whence); 41 version(offset64) 42 return AAsset_seek64(asset, count, SEEK_CUR); 43 else 44 return AAsset_seek(asset, cast(int)count, SEEK_CUR); 45 } 46 bool write(string path, void[] data) 47 { 48 return false; 49 } 50 void close() 51 { 52 if(asset != null) 53 AAsset_close(asset); 54 } 55 } 56 57 class HipAndroidFileSystemInteraction : IHipFileSystemInteraction 58 { 59 bool read(string path, void delegate(ubyte[] data) onSuccess, void delegate(string err) onError) 60 { 61 ubyte[] output; 62 HipAndroidFile f = new HipAndroidFile(path, FileMode.READ); 63 output.length = f.size; 64 bool ret = f.read(output.ptr, f.size) >= 0; 65 f.close(); 66 destroy(f); 67 if(!ret) 68 onError("Could not read file."); 69 else 70 onSuccess(output); 71 return ret; 72 } 73 bool write(string path, void[] data) 74 { 75 HipAndroidFile f = new HipAndroidFile(path, FileMode.WRITE); 76 bool ret = f.write(path, data); 77 f.close(); 78 destroy(f); 79 return ret; 80 } 81 bool exists(string path) 82 { 83 HipAndroidFile f = new HipAndroidFile(path, FileMode.WRITE); 84 bool x = f.asset != null; 85 f.close(); 86 destroy(f); 87 return x; 88 } 89 bool remove(string path) 90 { 91 return false; 92 } 93 94 bool isDir(string path) 95 { 96 return false; 97 } 98 99 } 100 }